<?php

class Skjb_VersionControl_Subversion_WorkingCopy_Resource
{
    protected $workingCopy = null;
    
    protected $path = null;
    
    protected $isDirectory = false;
    
    protected $isVersionControlled = false;
    
    protected $revision = null;
    
    protected $remotePath = null;
    
    protected $remoteBase = null;
    
    protected $lastChangedDate = null;
    
    protected $lastChangedRevision = null;
    
    protected $lastChangedAuthor = null;
    
    protected $uuid = null;

    public function __construct($path, $isDirectory, $workingCopy)
    {
        $this->path = $path;
        $this->isDirectory = $isDirectory;
        $this->workingCopy = $workingCopy;
        
        /*$entriesPath = $this->path . DIRECTORY_SEPARATOR . '.svn' . DIRECTORY_SEPARATOR . 'entries';
        if (file_exists($entriesPath) && is_file($entriesPath)) {
            $contents = file_get_contents($entriesPath);
            $lines = explode("\n", $contents);
            
            $this->revision = (int)$lines[3];
            $this->remotePath = $lines[4];
            $this->remoteBase = $lines[5];
            
            $this->lastChangedDate = $lines[9];
            $this->lastChangedRevision = $lines[10];
            $this->lastChangedAuthor = $lines[11];
            
            $this->uuid = $lines[26];
            
            $this->isVersionControlled = true;
        }*/
    }
    
    public function isDirectory()
    {
        return $this->isDirectory;
    }
    
    public function isVersionControlled()
    {
        return $this->isVersionControlled;
    }
    
    public function __get($name)
    {
        switch ($name) {
            case 'contents':
                return $this->getContents();
                break;
            case 'name':
            	return basename($this->path);
            	break;
        }
        
        /*
        URL: svn://vps.c4ssr.org/svn/10k/trunk
        Repository Root: svn://vps.c4ssr.org/svn/10k
        Repository UUID: ed46b2e1-6d38-0410-b77e-dbc361735a95
        Revision: 844
        Node Kind: directory
        Schedule: normal
        Last Changed Author: steven
        Last Changed Rev: 844
        Last Changed Date: 2010-03-19 21:50:49 +1000 (Fri, 19 Mar 2010)
        */
        
        /*
        URL: svn://vps.c4ssr.org/svn/10k/trunk/public_html/index2.php
        Repository Root: svn://vps.c4ssr.org/svn/10k
        Repository UUID: ed46b2e1-6d38-0410-b77e-dbc361735a95
        Revision: 844
        Node Kind: file
        Schedule: normal
        Last Changed Author: steven
        Last Changed Rev: 29
        Last Changed Date: 2009-08-03 10:04:51 +1000 (Mon, 03 Aug 2009)
        Text Last Updated: 2009-10-14 14:59:18 +1000 (Wed, 14 Oct 2009)
        Checksum: 88d3cf8cf4efa56dcd5d29b5323476dc
        */

        trigger_error('No such property "' . $name . '"');
    }

    public function getContents()
    {
        $entriesPath = $this->path . DIRECTORY_SEPARATOR . '.svn' . DIRECTORY_SEPARATOR . 'entries';
        
        if (file_exists($entriesPath) && is_file($entriesPath)) {
            $contents = file_get_contents($entriesPath);
            $lines = explode("\n", $contents);
            
            $resources = array();
            
            $currentFile = array();
            
            $inFiles = false;
            
            foreach ($lines as $line) {
                if ($line == chr(12)) {
                    if (!$inFiles) {
                        $inFiles = true;
                    } else {
                        if (!isset($currentFile[22]) || empty($currentFile[22])) {
                            $resources[] = new Skjb_VersionControl_Subversion_WorkingCopy_Resource($this->path . DIRECTORY_SEPARATOR . $currentFile[0], $currentFile[1] == 'dir', $this->workingCopy);
                        }
                        
                        $currentFile = array();
                    }
                } else {
                    if ($inFiles) {
                        $currentFile[] = $line;
                    }
                }
            }
            
            return $resources;
        }
    }
}